 #include "gm_sc_dev_mgr.h"
#include <middleware/mk_lib/mk_logger.h>

gm_sc_dev_mgr::gm_sc_dev_mgr(void)
{
}

gm_sc_dev_mgr::~gm_sc_dev_mgr(void)
{
	clear();
}


ULONG gm_sc_dev_mgr::add_dev(gm_sc_dev* sc_dev)
{
	return _sc_dev_list.add(sc_dev);
}

ULONG gm_sc_dev_mgr::remove_dev(gm_sc_dev* sc_dev)
{
	if(sc_dev)
	{
		DBG_LOGMSG("gm_sc_dev_mgr::remove_dev delete %x", sc_dev);
		delete sc_dev;
		DBG_LOGMSG("gm_sc_dev_mgr::remove_dev delete OK");
		_sc_dev_list.remove(sc_dev);
		DBG_LOGMSG("gm_sc_dev_mgr::list.remove OK");
		sc_dev = NULL;
	}
	return SAR_OK;
}


void gm_sc_dev_mgr::clear()
{
	ULONG lRslt = SAR_OK;
	mk_node* pHead = _sc_dev_list.get_head();
	while (pHead != NULL)
	{
		mk_node *node = pHead;
		gm_sc_dev* sc_dev = (gm_sc_dev*)node->get_data();

		gm_sc_app_mgr *sc_app = sc_dev->get_app_mgr();
		if(sc_app != NULL)
			sc_app->clear();

		delete sc_dev;
		pHead = node->get_next_node();
	}
	_sc_dev_list.remove_all();
	return ;
}

gm_sc_dev* gm_sc_dev_mgr::get_dev_by_handle( HANDLE devHandle )
{
	mk_node* pHead = _sc_dev_list.get_head();
	if(pHead == NULL) return NULL;

	gm_sc_dev* pdev = (gm_sc_dev *)pHead->get_data();
	if(pdev->get_handle() == devHandle)
	{
		return pdev;
	}

	mk_node* pNode = pHead->get_next_node();

	while(pNode != NULL)
	{
		pdev = (gm_sc_dev *)pNode->get_data();
		if(pdev->get_handle() == devHandle)
		{
			return pdev;
		}

		pNode = pNode->get_next_node();
	}
	return NULL; 
}

gm_sc_dev* gm_sc_dev_mgr::get_first_dev()
{
	_curr_pos = _sc_dev_list.get_head();
	if(_curr_pos == NULL)
	{
		return NULL;
	}
	return (gm_sc_dev *)_curr_pos->get_data();
}

gm_sc_dev* gm_sc_dev_mgr::get_next_dev()
{
	if(_curr_pos == NULL)
	{
		_curr_pos =  _sc_dev_list.get_head();
	}

	if(_curr_pos == NULL)
	{
		return NULL;
	}

	_curr_pos = _curr_pos->get_next_node();
	if(_curr_pos == NULL)
	{
		return NULL;
	}

	return (gm_sc_dev *)_curr_pos->get_data();
}


gm_sc_key * gm_sc_dev_mgr::find_key( HANDLE hkey, gm_sc_dev *& pDev, gm_sc_app *& pApp, gm_sc_cont *& pContainer)
{
	gm_sc_key * pKey = NULL;
	gm_sc_dev *sc_dev = get_first_dev();
	while(sc_dev)
	{
		pKey = sc_dev->find_key(hkey, pApp, pContainer);
		if(pKey)
		{
			pDev = sc_dev;
			break;
		}

		sc_dev = get_next_dev();
	}

	return pKey;
}


gm_sc_digest * gm_sc_dev_mgr::find_digest( HANDLE hHash, gm_sc_dev *& pDev )
{
	gm_sc_digest * sc_digest = NULL;
	gm_sc_dev *sc_dev = get_first_dev();
	while(sc_dev)
	{	
		sc_digest = sc_dev->find_digest(hHash);
		if(sc_digest)
		{
			pDev = sc_dev;
			return sc_digest;
		}

		sc_dev = get_next_dev();
	}

	return NULL;
}

gm_sc_mac * gm_sc_dev_mgr::find_mac( HANDLE hMac, gm_sc_dev *& pDev, gm_sc_app *& pApp, gm_sc_cont *& pContainer)
{
	gm_sc_mac * pMac = NULL;
	gm_sc_dev *sc_dev = get_first_dev();
	while(sc_dev)
	{
		pMac = sc_dev->find_mac(hMac, pApp, pContainer);
		if(pMac)
		{
			pDev = sc_dev;
			break;
		}

		sc_dev = get_next_dev();
	}

	return pMac;
}

gm_sc_cont * gm_sc_dev_mgr::find_container( HANDLE hContainer, gm_sc_dev *& pDev, gm_sc_app *& pApp )
{
	gm_sc_cont * pContainer = NULL;
	gm_sc_dev *sc_dev = get_first_dev();
	while(sc_dev)
	{
		pContainer = sc_dev->find_container(hContainer, pApp);
		if(pContainer)
		{
			pDev = sc_dev;
			break;
		}

		sc_dev = get_next_dev();
	}

	return pContainer;
}

gm_sc_app * gm_sc_dev_mgr::find_app( HAPPLICATION hApplication,gm_sc_dev *& pDev )
{
	gm_sc_app * sc_app = NULL;
	gm_sc_dev *sc_dev = get_first_dev();
	while(sc_dev)
	{	
		sc_app = sc_dev->find_app(hApplication);
		if(sc_app)
		{
			pDev = sc_dev;
			return sc_app;
		}
		
		sc_dev = get_next_dev();
	}

	return NULL;
}
